import * as index from '@/api/index.js'
export default {
	data() {
		return {
			title: "",
			con_id: '',
			value: '',
			range: [{
					value: 'today',
					text: "今日施工"
				},
				{
					value: 'res',
					text: "预约施工"
				}
			],
			tips: '',
			sex: [{
				text: '整车',
				value: 'all'
			}, {
				text: '局部',
				value: 'part'
			}],
			radio1: 0,
			sex1: [{
				text: '否',
				value: 0
			}, {
				text: '是',
				value: 1
			}],
			list: [],
			form: {
				con_id: '',
				work_time: '',
				type: '',
				shop_id: '',
				shop_name: '',
				part_type: '',
				part_ids: [],
				first: '', //车id
				second: '', //车型号id
				is_damage: 0,
				my_coupon_id: ''
				// use_coupon: 0
			},
			shopData: {}, //门店
			startTime: new Date().getTime(),
			endTime: '',
			partList: [],
			partName: [],
			carTypeList: [], //车辆型号
			carName: '',
			submitStatus: true,
			conList: [],
			coupon: {},
			listStatus: false,
			use_coupon: 'no',
			couponTitle:'',
			rules: {
				// 对name字段进行必填验证
				type: {
					rules: [{
						required: true,
						errorMessage: '请选择订单类型',
					}]
				},
				shop_name: {
					rules: [{
						required: true,
						errorMessage: '请添加施工门店',
					}]
				},
				part_type: {
					rules: [{
						required: true,
						errorMessage: '请选择施工部位',
					}]
				},
				second: {
					rules: [{
						required: true,
						errorMessage: '请选择施工车型',
					}]
				},
				work_time: {
					rules: [{
						required: true,
						errorMessage: '请选择技师到店时间',
					}]
				},
			},
			ps: {}
		}
	},
	onReady() {
		// 需要在onReady中设置规则
		this.$refs.form.setRules(this.rules)
	},
	onShow() {
		this.getMyShop()
	},
	onLoad(option) {
		let that = this
		this.title = option.title
		uni.setNavigationBarTitle({
			title: option.title
		});
		this.getMyCoupon()
		this.getCarBrandList()
		// this.getCarPartList()
		this.getConList(option.title)
		uni.$on('car', function(data) {
			console.log();
			let car = JSON.parse(data.msg)
			that.form.first = car.first
			that.form.second = car.second
			that.form.car_model = car.car_model
		})
		uni.$on('coupon', function(data) {
			that.form.my_coupon_id = data.id
			that.coupon.forEach(item => {
				if (item.id == data.id) {
					that.couponTitle = item.coupon_price
				}
			})
		})
	},

	methods: {
		jump(item) {
			this.$tools.navigate(item)
		},
		getMyCoupon() {
			index.getMyCoupon().then(res => {
				console.log(res.data);
				this.coupon = res.data
			})
		},
		typeChange(e) {
			console.log(e);
			this.form.type = e
			index.getCon().then(res => {
				// console.log(res.data);
				if (e == 'today') {
					this.tips = '预付' + res.data.today_deposit + '元,施工后补付工时费余款'
					const today = new Date();
					today.setHours(23, 59, 59, 999); // 设置到今天的最后一秒和毫秒
					this.endTime = today.getTime();
				} else if (e == 'res') {
					this.tips = '预付' + res.data.res_deposit + '元,施工后补付工时费余款'
					const today = new Date();
					today.setHours(23, 59, 59, 999); // 设置到今天的最后一秒和毫秒
					this.endTime = today.getTime() + (30 * 24 * 60 * 60 * 1000);
				} else {
					this.tips = ''
				}
			})
		},
		chooseCar() {
			this.$tools.navigate('/pages/home/car/index')
			// this.$refs.car.open('bottom')
		},
		carClick(e) {
			// this.form.car = e.item.name
			// this.$refs.car.close()
			console.log(e);
			this.getCarTypeList(e.item.name)
			this.$refs.car.close()
			this.$refs.carType.open('bottom')
		},
		getMyShop() {
			index.getMyShop().then(res => {
				this.shopData = res.data
				this.form.shop_id = res.data.id
				this.form.shop_name = res.data.name
			})
		},
		getCarBrandList() {
			index.getCarBrandList().then(res => {
				this.list = res.data
				this.listStatus = true
			})
		},
		//车辆型号
		getCarTypeList(name) {
			index.getCarTypeList({
				brand_name: name
			}).then(res => {
				this.carName = name
				this.carTypeList = res.data
			})
		},
		carTypeClick(item, index) {
			this.form.first = item.brand_id
			this.form.second = item.id
			this.form.car_model = this.carName + '/' + item.name
			this.$refs.carType.close()
		},
		//局部部件
		getCarPartList(id) {
			index.getCarPartList({
				con_id: id
			}).then(res => {
				console.log(res, 'res');
				this.partList = res.data.con_part_list
			})
		},
		partChange(e) {
			if (e.detail.value != 'part') {
				this.form.part_ids = []
				this.partName = []
			}
		},
		partClick(item, index) {
			if (this.form.part_ids.indexOf(item.id) < 0) {
				this.form.part_ids.push(item.id)
				this.partName.push(item.name)
			} else {
				this.removeByValue(this.form.part_ids, item.id)
				this.removeByValue(this.partName, item.name)
			}
		},
		removeByValue(arr, val) {
			for (var i = 0; i < arr.length; i++) {
				if (arr[i] == val) {
					arr.splice(i, 1);
					break;
				}
			}
		},
		switchChange(e) {
			console.log(e);
			if (e.detail.value) {
				this.form.use_coupon = 1
			} else {
				this.form.use_coupon = 0
			}
		},
		submit() {
			let that = this
			// if (this.submitStatus) {
			// 	this.submitStatus = false
			let form = JSON.parse(JSON.stringify(this.form))
			form.part_ids = form.part_ids.join()

			// let that = this
			uni.showModal({
				title: '',
				content: '是否确认提交?',
				success: function(res) {
					if (res.confirm) {
						that.$refs.form.validate().then(res => {
							uni.hideLoading()
							index.addOrder(form).then(res => {
								uni.requestPayment({
									provider: res.data.provider,
									timeStamp: res.data.timeStamp,
									nonceStr: res.data.nonceStr,
									package: res.data.package,
									signType: res.data.signType,
									paySign: res.data.paySign,
									success: function(res) {
										that.$tools.toast('支付成功')
										setTimeout(() => {
											// uni.reLaunch({
											// 	url: '/pages/user/order/index'
											// });
											that.$tools.navigate(
												'/pages/user/order/index',
												true)
										})
									},
									fail: function(err) {
										that.$tools.toast('支付失败')
									}
								});
								// this.submitStatus = true
								// setTimeout(() => {
								// 	this.submitStatus = true
								// }, 3000)
								console.log(res, 'res');
							})
						}).catch(err => {
							uni.hideLoading()
							console.log('表单错误信息：', err);
						})

					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});

			// }

		},
		getConList(title) {
			console.log(title);
			index.getConList().then(res => {
				this.conList = res.data
				res.data.forEach(item => {
					this.ps = item
					if (item.name == title) {
						this.use_coupon = item.use_coupon
						this.form.con_id = item.id
						this.getCarPartList(item.id)
					}
				})
			})
		}
	}
}